﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 文档类型。
 * 
 * 最后修改：2011-10-19
 * ************************************************************/
namespace Mozlite
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Globalization;
    using System.IO;
    /// <summary>
    /// 文档类型。
    /// </summary>
    public class MimeTypes
    {
        private MimeTypes() { }

        private class MimeType {
            public string Type { get; set; }
            public FileType FileType { get; set; }
        }

        private static Dictionary<string, MimeType> types = new Dictionary<string, MimeType>(StringComparer.OrdinalIgnoreCase){
            {"css", new MimeType(){ Type ="text/css", FileType = FileType.TEXT}},
            {"xml", new MimeType(){ Type ="text/xml", FileType = FileType.TEXT}},
            {"htm", new MimeType(){ Type ="text/html", FileType = FileType.TEXT}},
            {"html", new MimeType(){ Type ="text/html", FileType = FileType.TEXT}},
            {"txt", new MimeType(){ Type ="text/plain", FileType = FileType.TEXT}},
            {"js", new MimeType(){ Type ="text/javascript", FileType = FileType.TEXT}},
            {"gif", new MimeType(){ Type ="image/gif", FileType = FileType.Image}},
            {"bmp", new MimeType(){ Type ="image/bmp", FileType = FileType.Image}},
            {"png", new MimeType(){ Type ="image/png", FileType = FileType.Image}},
            {"jpg", new MimeType(){ Type ="image/jpeg", FileType = FileType.Image}},
            {"jpeg", new MimeType(){ Type ="image/jpeg", FileType = FileType.Image}},
            {"mpeg", new MimeType(){ Type ="video/mpeg", FileType = FileType.Image}},
            {"mpg", new MimeType(){ Type ="video/mpeg", FileType = FileType.Image}},
            {"mpa", new MimeType(){ Type ="video/mpeg", FileType = FileType.Image}},
            {"mpe", new MimeType(){ Type ="video/mpeg", FileType = FileType.Image}},
            {"wmv", new MimeType(){ Type ="video/x-ms-wmv", FileType = FileType.Video}},
            {"wma", new MimeType(){ Type ="video/x-ms-wmv", FileType = FileType.Video}},
            {"qt", new MimeType(){ Type ="video/quicktime", FileType = FileType.Video}},
            {"asf", new MimeType(){ Type ="video/x-ms-asf", FileType = FileType.Video}},
            {"asr", new MimeType(){ Type ="video/x-ms-asf", FileType = FileType.Video}},
            {"asx", new MimeType(){ Type ="video/x-ms-asf", FileType = FileType.Video}},
            {"mov", new MimeType(){ Type ="video/quicktime", FileType = FileType.Video}},
            {"avi", new MimeType(){ Type ="video/x-msvideo", FileType = FileType.Video}},
            {"pdf", new MimeType(){ Type ="application/pdf", FileType = FileType.Application}},
            {"doc", new MimeType(){ Type ="application/msword", FileType = FileType.Application}},
            {"rtf", new MimeType(){ Type ="application/msword", FileType = FileType.Application}},
            {"rss", new MimeType(){ Type ="application/rss+xml", FileType = FileType.Application}},
            {"csv", new MimeType(){ Type ="application/vnd.ms-excel", FileType = FileType.Application}},
            {"xls", new MimeType(){ Type ="application/vnd.ms-excel", FileType = FileType.Application}},
            {"ppt", new MimeType(){ Type ="application/vnd.ms-powerpoint", FileType = FileType.Application}},
            {"swf", new MimeType(){ Type ="application/x-shockwave-flash", FileType = FileType.Application}},
        };
        /// <summary>
        /// 通过MimeType获取扩展名称。
        /// </summary>
        /// <param name="mimeType">MimeType类型字符串。</param>
        /// <returns>返回扩展名。</returns>
        public static string GetExtension(string mimeType)
        {
            var mtypes = from type in types
                         where type.Value.Type.IsMatched(mimeType)
                         select type.Key;
            if(mtypes == null || mtypes.Count<string>() == 0)
                return "unknown";
            return mtypes.ElementAt<string>(0);
        }

        private static MimeType GetMType(string extension) {
            MimeType type;
            types.TryGetValue(extension, out type);
            return type;
        }

        /// <summary>
        /// 通过文件名返回MimeType字符串。
        /// </summary>
        /// <param name="fileName">文件名或扩展名。</param>
        /// <returns>返回MimeType字符串。</returns>
        public static string GetMimeType(string fileName)
        {
            string extension = CommonHelper.GetExtension(fileName);
            MimeType type = GetMType(extension);
            if(type == null)
                return "application/octet-stream";
            return type.Type;
        }
        /// <summary>
        /// 判断是否图片类型。
        /// </summary>
        /// <param name="fileName">文件名称。</param>
        /// <returns>返回是否为图片。</returns>
        public static FileType GetFileType(string fileName)
        {
            string extension = CommonHelper.GetExtension(fileName);
            if (string.IsNullOrEmpty(extension))
                return FileType.Unknown;
            MimeType type = GetMType(extension);
            if (type == null)
            {
                switch (extension.ToLower())
                { 
                    case "exe":
                        return FileType.EXE;
                    case "rar":
                        return FileType.RAR;
                    case "swf":
                        return FileType.Flash;
                    default:
                        return FileType.Other;
                }
            }
            return type.FileType;
        }
        /// <summary>
        /// 通过文件扩展名返回MimeType字符串。
        /// </summary>
        /// <param name="extension">文件扩展名。</param>
        /// <param name="defaultType">默认文档类型。</param>
        /// <returns>返回文档类型。</returns>
        public static string GetContentType(string extension, string defaultType = "text/html")
        {
            MimeType type;
            if (types.TryGetValue(extension, out type))
                return type.Type;
            return defaultType;
        }
    }
}
